package com.whfc.base.config;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.third.sms.SmsApi;
import com.whfc.common.third.sms.impl.AliyunSmsApi;
import com.whfc.common.third.sms.impl.AliyunSmsProperties;
import com.whfc.common.third.weather.WeatherApi;
import com.whfc.common.third.weather.impl.CaiyunWeatherApi;
import com.whfc.common.third.weather.impl.CaiyunWeatherProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: likang
 * @Date: 2020-11-9 10:31
 */
@Configuration
@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "com.whfc.base.properties", autoRefreshed = true),
        @NacosPropertySource(dataId = "com.whfc.common.properties", autoRefreshed = true)
})
public class BaseConfig {

    @Value("${spring.profiles.active}")
    private String profile;

    @Bean("msFilePathConfig")
    public FilePathConfig msFilePathConfig() {
        FilePathConfig config = new FilePathConfig("ms", profile);
        return config;
    }

    /**
     * 阿里云-大陆短信接口参数
     *
     * @return
     */
    @Bean("baseSmsProps")
    @ConfigurationProperties("aliyun.sms.base")
    public AliyunSmsProperties aliyunBaseProps() {
        return new AliyunSmsProperties();
    }

    /**
     * 阿里云-大陆短信接口
     *
     * @return
     */
    @Bean("baseSmsApi")
    public SmsApi baseSmsApi(@Qualifier("baseSmsProps") AliyunSmsProperties properties) {
        SmsApi smsApi = new AliyunSmsApi(properties.getAccessKeyId(), properties.getAccessSecret(), properties.getSign(), properties.getTemplate());
        return smsApi;
    }

    /**
     * 阿里云-港澳台短信接口参数
     *
     * @return
     */
    @Bean("hmtProps")
    @ConfigurationProperties("aliyun.sms.hmt")
    public AliyunSmsProperties aliyunHmtProps() {
        return new AliyunSmsProperties();
    }

    /**
     * 港澳台-短信接口
     *
     * @param properties
     * @return
     */
    @Bean("hmtSmsApi")
    public SmsApi hmtSmsApi(@Qualifier("hmtProps") AliyunSmsProperties properties) {
        SmsApi smsApi = new AliyunSmsApi(properties.getAccessKeyId(), properties.getAccessSecret(), properties.getSign(), properties.getTemplate());
        return smsApi;
    }

    /**
     * 彩云天气API-参数
     *
     * @return
     */
    @Bean("weatherProps")
    @ConfigurationProperties("caiyun")
    public CaiyunWeatherProperties weatherProps() {
        return new CaiyunWeatherProperties();
    }

    /**
     * 彩云天气API-参数
     *
     * @param properties
     * @return
     */
    @Bean("weatherApi")
    public WeatherApi weatherApi(@Qualifier("weatherProps") CaiyunWeatherProperties properties) {
        return new CaiyunWeatherApi(properties.getToken());
    }
}
